package threadDemo;

import java.util.TreeSet;

/**
 * 龟兔赛跑
 */
public class Test7 implements Runnable{

    private String victory;

    public boolean pk(int i) {
        String name = Thread.currentThread().getName();
        if(name=="兔子"&&0==i%10){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {

            }
        }
        if(victory==null){
            if(i==0){
                this.victory=name;
                System.out.println("winner is "+name);
                return true;
             }
            return false;
        }
        return true;
    }

    @Override
    public void run() {
        for(int i=100;i>=0 ; i--){
            boolean pk = pk(i);
            if(pk){
                break;
            }
            System.out.println(Thread.currentThread().getName()+"距离赛道还有"+i+"米");
        }
    }

    public static void main(String[] args) {
        Test7 t = new Test7();
        new Thread(t,"兔子").start();
        new Thread(t,"乌龟").start();
    }
}
